﻿using AntdUI;
using CardRegisterCollection.Model;
using CardRegisterCollection.Utility;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace _9jCardRegisterCollection
{
    public partial class FrmConfigSync : AntdUI.Window
    {
        public FrmConfigSync()
        {
            InitializeComponent();
        }

        private static string TempSettingFolderName = "信息采集系统配置";
        private string LocalConnectionString = "Provider=Microsoft.ACE.OleDb.12.0;Data Source=" + Environment.CurrentDirectory + "\\Db\\main.mdb;Persist Security Info=False";
        private static string SourceDbPath = Environment.CurrentDirectory + "\\Temp\\" + TempSettingFolderName + "\\main.mdb";
        private string SourceConnectionString = "Provider=Microsoft.ACE.OleDb.12.0;Data Source=" + SourceDbPath + ";Persist Security Info=False";


        private CardRegisterCollection.BLL.CardType _cardTypeBll = new CardRegisterCollection.BLL.CardType();
        private CardRegisterCollection.BLL.Unit _unitBll = new CardRegisterCollection.BLL.Unit();
        private CardRegisterCollection.BLL.Role _roleBll = new CardRegisterCollection.BLL.Role();
        private CardRegisterCollection.BLL.RoleMenu _roleMenuBll = new CardRegisterCollection.BLL.RoleMenu();
        private CardRegisterCollection.BLL.User _userBll = new CardRegisterCollection.BLL.User();
        private CardRegisterCollection.BLL.UserRole _userRoleBll = new CardRegisterCollection.BLL.UserRole();


        /// <summary>
        /// 选择配置文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfigFileUpload_Click(object sender, EventArgs e)
        {
            var openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "配置压缩包(*.zip)|*.zip";
            openFileDialog.Title = "选择配置文件";
            openFileDialog.Multiselect = false;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                var fileName = openFileDialog.FileName;
                ZipUtility.UnZip(fileName, Environment.CurrentDirectory + "\\Temp\\" + TempSettingFolderName, PubConstant.AppZipPassword);

                if (!File.Exists(SourceDbPath))
                {
                    AntdUI.Message.error(this, "配置包有问题，请检查配置包重新上传", autoClose: 3);
                    return;
                }

                DataSync();
            }
        }



        private void DataSync()
        {
            PubConstant.ConnectionString = SourceConnectionString;

            AntdUI.Spin.open(this, new AntdUI.Spin.Config()
            {
                Back = Color.FromArgb(220, 147, 181, 207),
                Color = Style.Db.Primary,//转圈颜色
                Radius = 6,
                Fore = Color.Black,//字体颜色
                Font = new Font("Microsoft YaHei UI", 14f),//字体可以控制进度圈的大小
            }, (config) =>
            {

                try
                {
                    config.Text = "数据准备中...";
                    var _cardTypeList = _cardTypeBll.GetModelList("");
                    var _unitList = _unitBll.GetModelList("");
                    var _roleList = _roleBll.GetList("");
                    var _roleMenuList = _roleMenuBll.GetModelList("");
                    var _userList = _userBll.GetList("");
                    var _userRoleList = _userRoleBll.GetModelList("");
                    config.Text = "正在同步,当前进度20%...";
                    config.Value = 0.2f;
                    // 同步证件类型
                    DataSync(_cardTypeList);
                    config.Text = "正在同步,当前进度30%...";
                    config.Value = 0.3f;
                    // 同步单位部门
                    DataSync(_unitList);
                    config.Text = "正在同步,当前进度40%...";
                    config.Value = 0.4f;
                    // 同步角色
                    DataSync(_roleList);
                    config.Text = "正在同步,当前进度50%...";
                    config.Value = 0.5f;
                    // 同步角色
                    DataSync(_roleMenuList);
                    config.Text = "正在同步,当前进度60%...";
                    config.Value = 0.6f;
                    // 同步用户
                    DataSync(_userList);
                    config.Value = 0.7f;
                    config.Text = "正在同步,当前进度70%...";
                    // 同步用户授权
                    DataSync(_userRoleList);
                    config.Value = 1f;
                    config.Text = "正在同步,当前进度100%...";
                }
                catch (Exception e)
                {
                    config.Text = "同步失败...";
                    Thread.Sleep(2000);
                }
            }, () =>
            {
                AntdUI.Message.success(this, "配置同步完成", autoClose: 3);
            });



        }

        /// <summary>
        /// 同步证件类型
        /// </summary>
        /// <param name="cardTypes">源证件类型</param>
        private void DataSync(List<CardType> cardTypes)
        {
            foreach (var cardType in cardTypes)
            {
                var exist = _cardTypeBll.Exists($" and [ID] = '{cardType.Id}'");
                if (exist)
                {
                    // 更新
                    _cardTypeBll.Update(cardType);
                }
                else
                {
                    // 新增
                    _cardTypeBll.Add(cardType);
                }
            }
        }

        /// <summary>
        /// 同步单位部门
        /// </summary>
        /// <param name="units">源单位</param>
        private void DataSync(List<Unit> units)
        {
            foreach (var unit in units)
            {
                var exist = _unitBll.Exists($" and [ID] = '{unit.ID}'");
                if (exist)
                {
                    // 更新
                    _unitBll.Update(unit);
                }
                else
                {
                    // 新增
                    _unitBll.Add(unit);
                }
            }
        }

        /// <summary>
        /// 同步角色
        /// </summary>
        /// <param name="roles">源角色</param>
        private void DataSync(List<Role> roles)
        {
            foreach (var role in roles)
            {
                var exist = _roleBll.Exists($" and [ID] = '{role.ID}'");
                if (exist)
                {
                    // 更新
                    _roleBll.Update(role);
                }
                else
                {
                    // 新增
                    _roleBll.Add(role);
                }
            }
        }


        /// <summary>
        /// 同步角色菜单
        /// </summary>
        /// <param name="roleMenus">源角色菜单</param>
        private void DataSync(List<RoleMenu> roleMenus)
        {
            foreach (var roleMenu in roleMenus)
            {
                var exist = _roleMenuBll.Exists($" and [ID] = '{roleMenu.ID}'");
                if (exist)
                {
                    // 更新

                    _roleMenuBll.Update(roleMenu);
                }
                else
                {
                    // 新增
                    _roleMenuBll.Add(roleMenu);
                }
            }
        }


        /// <summary>
        /// 同步用户
        /// </summary>
        /// <param name="users">源用户</param>
        private void DataSync(List<UserExtension> users)
        {
            foreach (var user in users)
            {
                var exist = _userBll.Exists($" and [ID] = '{user.ID}'");
                if (exist)
                {
                    // 更新
                    _userBll.Update(user);
                }
                else
                {
                    // 新增
                    _userBll.Add(user);
                }
            }
        }


        /// <summary>
        /// 同步用户角色
        /// </summary>
        /// <param name="userRoles">源用户角色</param>
        private void DataSync(List<UserRole> userRoles)
        {
            foreach (var userRole in userRoles)
            {
                var exist = _userRoleBll.Exists($" and [ID] = '{userRole.ID}'");
                if (!exist)
                {
                    // 新增
                    _userRoleBll.Add(userRole);
                }
            }
        }

        private void ConfigFileUpload_DragChanged(object sender, AntdUI.StringsEventArgs e)
        {
            var fileNames = e.Value;
        }
    }
}
